package com.iyao_app.mvp.preview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;
import android.widget.TextView;

import com.iyao_app.R;
import com.iyao_app.base.BaseActivity;
import com.iyao_app.entry.PreviewData;
import com.iyao_app.mvp.preview.fragment.FragmentPagerItems;
import com.iyao_app.mvp.preview.fragment.ImagePreviewFragment;
import com.iyao_app.util.Contants;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/12/12.
 */

public class ImageViewPagerActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.tv_count)
    TextView mTvCount;
    private int count = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        setContentView(R.layout.activity_perview);
        ButterKnife.bind(this);
        init();
    }

    private void init() {

        PreviewData previewData = (PreviewData) getIntent().getSerializableExtra(Contants.CLASS_TYPE);
        if (previewData != null && previewData.image != null && previewData.image.size() > 0) {

            FragmentPagerItems.Creator add = FragmentPagerItems.with(this);
            if (previewData.image != null && previewData.image.size() > 0) {
                for (String smartItem : previewData.image) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Contants.TYPE, smartItem);
                    add = add.add(smartItem, ImagePreviewFragment.class, bundle);
                }
                PreviewAdapter adapter = new PreviewAdapter(
                        getSupportFragmentManager(), add
                        .create());
                mViewpager.setAdapter(adapter);

                mViewpager.setCurrentItem(previewData.position);
                count = previewData.image.size();
                mTvCount.setText(previewData.position+1 + "/" + count);
            }
        }
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTvCount.setText(position+1 + "/" + count);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
